package com.bkav.portal.portlet.product.comparator;

import com.bkav.portal.portlet.product.model.Product;

import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.kernel.util.OrderByComparator;

public class ProductCreateDateComparator extends OrderByComparator {

	public static final String ORDER_BY_ASC = "Product.productId ASC";

	public static final String ORDER_BY_DESC = "Product.productId DESC";

	public static final String[] ORDER_BY_FIELDS = {"productId"};

	public ProductCreateDateComparator() {

		this(false);
	}

	public ProductCreateDateComparator(boolean ascending) {

		_ascending = ascending;
	}

	@Override
	public int compare(Object obj1, Object obj2) {

		Product product1 = (Product) obj1;
		Product product2 = (Product) obj2;

		int value = DateUtil.compareTo(product1.getCreateDate(), product2.getCreateDate());

		if (_ascending) {
			return value;
		}
		else {
			return -value;
		}
	}

	@Override
	public String getOrderBy() {

		if (_ascending) {
			return ORDER_BY_ASC;
		}
		else {
			return ORDER_BY_DESC;
		}
	}

	@Override
	public String[] getOrderByFields() {

		return ORDER_BY_FIELDS;
	}

	@Override
	public boolean isAscending() {

		return _ascending;
	}

	private boolean _ascending;

}